package com.sky.service.impl;


import com.sky.mapper.ViolationRecordMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private ViolationRecordMapper violationRecordMapper;

    /**
     * 根据时间段统计违法数据
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        /**
         * 车辆闯红灯：当日车辆闯红灯数量
         * 行人闯红灯：当日行人闯红灯数量
         * 车辆超速：当日车辆超速数量
         * 违法总数：当日违法总数
         */

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //查询违法总数
        Integer totalViolation = violationRecordMapper.countByMap(map);

        // 查询各个违法类型的统计信息
        List<Map<String, Object>> violationTypeSumList = violationRecordMapper.sumByMap(map);

        Integer carRunRed = 0; // 车辆闯红灯
        Integer pedestrianRunRed = 0; // 行人闯红灯
        Integer speeding = 0; // 车辆超速

        for (Map<String, Object> item : violationTypeSumList) {
            String violationType = (String) item.get("violationType");
            Long totalAmount = (Long) item.get("totalAmount"); // 使用 Long 接收

            if ("4".equals(violationType)) {
                carRunRed = totalAmount != null ? totalAmount.intValue() : 0;
            } else if ("5".equals(violationType)) {
                pedestrianRunRed = totalAmount != null ? totalAmount.intValue() : 0;
            } else if ("1".equals(violationType)) {
                speeding = totalAmount != null ? totalAmount.intValue() : 0;
            }
        }

        return BusinessDataVO.builder()
                .carrunred(carRunRed)
                .overspeed(speeding)
                .runred(pedestrianRunRed)
                .totalviolation(totalViolation)
                .build();
    }
}
